home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!willen.demon.co.uk
- From: Adrian Parker <adrian@willen.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: 2d array of pointers to structures ?
- Date: Mon, 15 Apr 96 13:04:08 GMT
- Organization: Quondam Ltd
- Message-ID: <829573448snz@willen.demon.co.uk>
- Reply-To: adrian@willen.demon.co.uk
- X-NNTP-Posting-Host: willen.demon.co.uk
- X-Mail2News-Path: willen.demon.co.uk
-
-
- I need to keep a 2d array of structures, which I have to do at runtime
- as they array could be quite big, and the compiler can't cope with
- static arrays of the required size.
-
- Say I have a structure of..
-
- typedef struct {
- int id;
- long colour;
- } T_loc;
- T_loc *locptr;
-
- And two variables containing the number of rows and colums. How do I
- allocate the storage for the memory array, and how then do I reference
- it ?
-
- I know I need to allocate a pointer for each row, so..
-
- locptr = (T_loc *)malloc(rows);
-
- should allocate enough space to hold "rows" number of T_loc pointers.
- Then in each entry, I need to store a pointer to an array of columns..
- which is where I'm stuck..
-
- for (x = 0 ; x < rows ; x++)
- {
- *(locptr+x) = (T_loc)malloc(cols);
- }
-
- which I think should malloc "cols" number of T_loc variables, and store
- the returned pointer in the 'x'th locptr pointer array.. but obviously
- something is wrong as it won't compile.
-
- I then need to be able to refer to a specific struct items for a
- specific column in a specific row..
-
- (T_loc *)((*(locptr+ROW))+COL)->id
-
- or am I way off here too ?
-
-
- Any help would be appreciated.
-
- Regards,
- confused of willenhall
- --
- :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
- :: Adrian Parker adrian@willen.demon.co.uk Quondam Ltd ::
- :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
-